package com.example.administrator.boltshow;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.CarBaseAdapter;
import application.MyApplication;
import model.Car;

public class CarActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private TextView mTextViewPay, mTextViewAll, mTextViewSub;
    private ListView mListView;
    private RadioButton mRadioButtonOther, mRadioButtonCommit;
    private List<Car> mData;
    private CarBaseAdapter mAdapter;
    private DbManager mDbManager;
    private Button mButton;
    private LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        initView();
        initData();
        setData();
        setListener();
    }

    private void initView() {
        mButton = (Button) findViewById(R.id.bt_car_jump);
        mTextViewAll = (TextView) findViewById(R.id.tv_car_allpay);
        mTextViewPay = (TextView) findViewById(R.id.tv_car_pay);
        mTextViewSub = (TextView) findViewById(R.id.tv_car_deletepay);
        mListView = (ListView) findViewById(R.id.lv_car_list);
        mRadioButtonCommit = (RadioButton) findViewById(R.id.rb_car_commit);
        mRadioButtonOther = (RadioButton) findViewById(R.id.rb_car_other);
        mLinearLayout = (LinearLayout) findViewById(R.id.ll_car_container);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new CarBaseAdapter(mData, this);
        DbManager.DaoConfig config = new DbManager.DaoConfig();
        mDbManager = x.getDb(config);
        //判断数据是否为空
        showEmpty();
    }

    private void setListener() {
        //给adapter设置一个数据改变的监听
        mAdapter.registerDataSetObserver(new DataSetObserver() {
            //在你调用adapter的notifiedDataChange的时候就调用这个方法
            @Override
            public void onChanged() {
                super.onChanged();
                int money = mAdapter.getMoney();
                mTextViewAll.setText("总额:¥ " + money);
                mTextViewSub.setText("在减:¥ " + 0);
                mTextViewPay.setText("实付款: ¥" + (money - 0));
            }
        });
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // startActivity(new Intent(CarActivity.this,));
                finish();
            }
        });
        mRadioButtonOther.setOnCheckedChangeListener(this);
        mRadioButtonCommit.setOnCheckedChangeListener(this);
    }

    private void setData() {
        //设置空布局
        View emptyView = LayoutInflater.from(this).inflate(R.layout.empty_car, null);
        mListView.setEmptyView(emptyView);
        //设置头布局
        View headView = LayoutInflater.from(this).inflate(R.layout.view_head_car, null);
        mListView.addHeaderView(headView);
        //设置脚布局
        View footView = LayoutInflater.from(this).inflate(R.layout.view_foot_car, null);
        mListView.addFooterView(footView);
        mListView.setAdapter(mAdapter);
        //获取用户名
        MyApplication application = (MyApplication) getApplication();
        String userName = application.getUserName();
        //从数据库里面读取数据,根据用户名
        try {
            List<Car> data = mDbManager.selector(Car.class).where("userName", "=", userName).findAll();
            if (data != null && data.size() != 0) {
                mData.addAll(data);
                Log.i("TAG", "mdata111"+mData.size());
                mAdapter.notifyDataSetChanged();
                showEmpty();
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {
        if (v != null) {
            Integer position = (Integer) v.getTag();
            switch (v.getId()) {
                case R.id.iv_item_car_add:
                    if (position != null) {
                        mData.get(position).count++;
                    }
                    break;
                case R.id.iv_item_car_sub:
                    if (position != null) {
                        if (mData.get(position).count == 1) {
                            return;
                        }
                        mData.get(position).count--;
                    }
                    break;
                case R.id.iv_item_car_delete:
                    if (position != null) {
                        Car car = mData.get(position);
                        mData.remove(mData.get(position));
                        mData.remove(position);
                        showEmpty();
                        //同时从数据库中删除:
                        try {
                            int delete = mDbManager.delete(Car.class, WhereBuilder.b("userName", "=", car.userName).and("goodName", "=", car.goodName).and("size", "=", car.size).and("color", "=", car.color));
                        } catch (DbException e) {
                            e.printStackTrace();
                        }

                    }
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_item_car_check:
                Integer position = (Integer) buttonView.getTag();
                if (position != null) {
                    mData.get(position).isCheck = isChecked;
                }
                break;
            case  R.id.rb_car_commit:
                Toast.makeText(CarActivity.this, "订单已成功提交", Toast.LENGTH_SHORT).show();
            case R.id.rb_car_other:
                Toast.makeText(CarActivity.this, "你好没有好友", Toast.LENGTH_SHORT).show();
            default:
                break;
        }
        mAdapter.notifyDataSetChanged();
    }

    //判断是否显示空的时候界面
    public void showEmpty() {
        if (mData.size() == 0) {
            mLinearLayout.setVisibility(View.VISIBLE);
            mTextViewAll.setText("总额:¥ xxx");
            mTextViewSub.setText("在减:¥ xxx");
            mTextViewPay.setText("实付款:¥ xxx");
        } else {
            mLinearLayout.setVisibility(View.GONE);
        }
    }


}
